#pragma once

#include "ftp/Types.h"

#include <queue>
#include <memory>
#include <boost/ptr_container/ptr_vector.hpp>

namespace ftp
{

class DataProducer; 
class DtpObserver;

class DTPManager
{
public:
	DTPManager(ba::io_service* ioService, int port);
	virtual ~DTPManager() {}

	virtual int getPort();
	virtual std::string getIp();

	virtual void acceptDtpWithDataProducer(std::auto_ptr<DataProducer> dataProducer, DtpObserver* dtpObserver);

private:
	void acceptNewConnection();
	void onAcceptNewConnection(SocketSPtr socket, const boost::system::error_code& error);
	void tryToCreateDtp();

private:
	struct DataConnection
	{
		SocketSPtr socket;
		std::auto_ptr<DataProducer> dataProducer;
		DtpObserver* observer;
	};

private:
	int m_port;
	ba::io_service* m_ioService;
	tcp::acceptor m_acceptor;

	std::queue<SocketSPtr> m_acceptedSockets;
	std::queue<DtpObserver*> m_queuedObservers;
	boost::ptr_vector<DataProducer> m_queuedDataProviders;

	boost::ptr_vector<DataConnection> m_dataConnections;
};

}

